package com.outsourcing.bottle.ui;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.CityEntry;
import com.outsourcing.bottle.domain.CityInfo;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.UICore;
/**
 * 
 * @author 06peng
 *
 */
public class SearchCityActivity extends BasicActivity implements BasicUIEvent, Callback, OnClickListener {
	
	private final int search_city = 1;
	private final int error = 2;
	private Handler handler;
	private List<CityEntry> cityList;
	
	private ImageView backView;
	private TextView titleView;
	private EditText editView;
	private ImageView searchView;
	private ListView listView;
	
	private String title;
	private int resultCode;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.search_city);
		handler = new Handler(this);
		title = getIntent().getStringExtra("title");
		resultCode = getIntent().getIntExtra("resultCode", 0);
		initWidget();
	}
	
	private void initWidget() {
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		titleView = (TextView) findViewById(R.id.topbar_title);
		titleView.setText(title);
		editView = (EditText) findViewById(R.id.search_city_edit);
		editView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
					UICore.eventTask(SearchCityActivity.this, SearchCityActivity.this, search_city, "search_city", null);
				}
				return false;
			}
		});
		searchView = (ImageView) findViewById(R.id.search_city_go);
		searchView.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.search_city_list);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				CityEntry entry = cityList.get(arg2);
				Intent intent = new Intent(SearchCityActivity.this, SettingBasicActivity.class);
				intent.putExtra("cityid", entry.getCityid());
				intent.putExtra("city", entry.getCity());
				intent.putExtra("province", entry.getProvince());
				intent.putExtra("country", entry.getCountry());
				setResult(resultCode, intent);
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (v == backView) {
			finish();
		} else if (v == searchView) {
			UICore.eventTask(this, this, search_city, "search_city", null);
		}
	}

	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case search_city:
			searchCity();
			break;

		default:
			break;
		}
	}
	
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case search_city:
			CityAdapter adapter = new CityAdapter();
			listView.setAdapter(adapter);
			break;
		case error:
			onToast((String) msg.obj);
			break;
		default:
			break;
		}
		return false;
	}
	
	private void searchCity() {
		String url = UrlConfig.search_city;
		HashMap<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("login_uid", String.valueOf(AppContext.getInstance().getLogin_uid()));
		paramsMap.put("login_token", AppContext.getInstance().getLogin_token());
		paramsMap.put("page", "1");
		paramsMap.put("count", "20");
		paramsMap.put("keyword", editView.getText().toString().trim());
		try {
			String result = HttpUtils.doPost(this, url, paramsMap);
			if (result != null) {
				Gson gson = new Gson();
				CityInfo info = gson.fromJson(result, CityInfo.class);
				if (info.getResults().getAuthok() == 0) {
					Message msg = Message.obtain(handler, error, info.getResults().getErrmsg());
					handler.sendMessage(msg);
				} else {
					cityList = info.getCities_list();
					handler.sendEmptyMessage(search_city);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public class CityAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return cityList == null ? 0 : cityList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return cityList == null ? null : cityList.get(arg0);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.search_city_listitem, null);
			}
			TextView city = (TextView) convertView.findViewById(R.id.search_city_listitem_city);
			TextView province = (TextView) convertView.findViewById(R.id.search_city_listitem_province);
			TextView country = (TextView) convertView.findViewById(R.id.search_city_listitem_country);
			CityEntry entry = cityList.get(position);
			if (entry != null) {
				city.setText(entry.getCity());
				province.setText(entry.getProvince());
				country.setText(entry.getCountry());
			}
			return convertView;
		}
		
	}

}
